package u1.common;


import java.io.*;

public class Book implements Serializable {
    private String type;
    private String path;
    private String title;
    private String size;
    private String author;
    private String content;
    private String name;
    private byte[] bytes;

    public Book(String title, String path, String author) {
        this.title = title;
        this.path = path;
        this.author = author;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public byte[] getBytes() {
        return bytes;
    }

    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

    public File readFile() {
        try {
            File file = new File(path);
            size = ((double) file.length() / 1024) + "KB";
            return file;
        } catch (Exception e) {
            System.out.println("路径不存在");
            return null;
        }
    }

    public byte[] readcontent() {
        File file = readFile();
        if (file == null) {
            return null;
        }
        BufferedInputStream bu = null;
        StringBuffer str = null;
        try {
            bu = new BufferedInputStream(new FileInputStream(file));
            bytes = new byte[bu.available()];
            while ((bu.read(bytes))!=-1) {
            }
            return bytes;
        } catch (Exception e) {
            return null;
        }
    }

    public byte[] readcontent1() {
        File file = readFile();
        if (file == null) {
            return null;
        }
        BufferedReader bu = null;
        StringBuffer str = null;
        try {
            bu = new BufferedReader(new FileReader(file));
            while ((content = bu.readLine()) != null) {
                str.append(content);
            }
            content = str.toString();
            return bytes;
        } catch (Exception e) {
            return null;
        }
    }


    public void addType(String str) {
        StringBuffer stringBuffer = new StringBuffer();
        if (type == null) {
            type = str;
            return;
        }
        stringBuffer.append(type);
        stringBuffer.append("||");
        stringBuffer.append(str);
        type = stringBuffer.toString();
    }

    @Override
    public String toString() {
        return "Book{" +
                "type='" + type + '\'' +
                ", title='" + title + '\'' +
                ", size='" + size + '\'' +
                ", author='" + author + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}